/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mnstudent.presentationlayer;

import mnstudent.businessLayer.StaffBLL;
import mnstudent.datatranferobject.LoginDTO;
import mnstudent.global.Standard;
import mnstudent.security.MD5;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

/**
 *
 * @author 11520_000
 */
public class frmChangePassword extends javax.swing.JDialog {

    private StaffBLL m_StaffBLL;
    /**
     * Creates new form frmChangePassword
     */
    public frmChangePassword(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(null);
        setMaxLenghtForTextField();
    }

     /**
     * Ham set max lenght lon nhat cho cac textField
     */
    public void setMaxLenghtForTextField()
    {
        m_StaffBLL = new StaffBLL();
        //TextField mat khau moi
        jPfPassNew.addKeyListener(new KeyAdapter() {
                public void keyTyped(KeyEvent e) {
                    int len = jPfPassNewAgain.getText().length();
                    if (len > Standard.MAX_LEN) {
                            e.consume();
                    }
                }
        });
        //TextField xac nhan lai mat khau
        jPfPassNewAgain.addKeyListener(new KeyAdapter() {
                public void keyTyped(KeyEvent e) {
                    int len = jPfPassNewAgain.getText().length();
                    if (len > Standard.MAX_LEN) {
                            e.consume();
                    }
                }
        });
        //TextField mat khau cu
        jPfPassOld.addKeyListener(new KeyAdapter() {
                public void keyTyped(KeyEvent e) {
                    int len = jPfPassOld.getText().length();
                    if (len > Standard.MAX_LEN) {
                            e.consume();
                    }
                }
        });
        
        UIManager.put("OptionPane.cancelButtonText", "Hủy");
        UIManager.put("OptionPane.noButtonText", "Không");
        UIManager.put("OptionPane.okButtonText", "Có");
        UIManager.put("OptionPane.yesButtonText", "Vâng");
    }
    
    private void checkChangePassword()
    {
        if(jPfPassOld.getText().equals("")||jPfPassNew.getText().equals("")||jPfPassNewAgain.equals(""))
            JOptionPane.showMessageDialog(null, "Dữ liệu không được bỏ trống!");
        else
        {
            String strOld = jPfPassOld.getText().trim();
            if(!LoginDTO.getPass().equals(MD5.Hash(strOld)))
            {
                 JOptionPane.showMessageDialog(null, "Nhập sai mật khẩu. Vui lòng nhập lại!");
            }
            else
            {
                 String strNew = jPfPassNew.getText().trim();
                 String strNewAgain = jPfPassNewAgain.getText().trim();
                if(!strNew.equals(strNewAgain))
                {
                     JOptionPane.showMessageDialog(null, "Sai mật khẩu xác thực. Vui lòng nhập lại!");
                }
                else
                {
                    
                    if(m_StaffBLL.updatePasswordOfStaffToDatabase(LoginDTO.getID(), MD5.Hash(strNew)))
                    {
                         LoginDTO.setPass(MD5.Hash(strNew));
                         JOptionPane.showMessageDialog(null, "Thay đổi mật khẩu thành công!");
                         this.dispose();
                    }
                    else
                    {
                          JOptionPane.showMessageDialog(null, "Thay đổi mật khẩu thất bại!");
                          
                    }
                }
            }
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jXPanel1 = new org.jdesktop.swingx.JXPanel();
        jXPanel2 = new org.jdesktop.swingx.JXPanel();
        jXLabel1 = new org.jdesktop.swingx.JXLabel();
        jXPanel3 = new org.jdesktop.swingx.JXPanel();
        jPfPassOld = new javax.swing.JPasswordField();
        jPfPassNew = new javax.swing.JPasswordField();
        jPfPassNewAgain = new javax.swing.JPasswordField();
        jXLabel2 = new org.jdesktop.swingx.JXLabel();
        jXLabel3 = new org.jdesktop.swingx.JXLabel();
        jXLabel4 = new org.jdesktop.swingx.JXLabel();
        jXPanel4 = new org.jdesktop.swingx.JXPanel();
        jxBtnCancel = new org.jdesktop.swingx.JXButton();
        jxBtnSave = new org.jdesktop.swingx.JXButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jXPanel1.setLayout(new java.awt.BorderLayout());

        jXLabel1.setForeground(new java.awt.Color(51, 102, 255));
        jXLabel1.setText("ĐỔI MẬT KHẨU");
        jXLabel1.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
        jXPanel2.add(jXLabel1);

        jXPanel1.add(jXPanel2, java.awt.BorderLayout.NORTH);

        jPfPassNewAgain.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jPfPassNewAgainKeyPressed(evt);
            }
        });

        jXLabel2.setText("Mật khẩu cũ");

        jXLabel3.setText("Xác nhận lại mật khẩu");

        jXLabel4.setText("Mật khẩu mới");

        javax.swing.GroupLayout jXPanel3Layout = new javax.swing.GroupLayout(jXPanel3);
        jXPanel3.setLayout(jXPanel3Layout);
        jXPanel3Layout.setHorizontalGroup(
            jXPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel3Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(jXPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jXLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jXPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPfPassNew, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE)
                    .addComponent(jPfPassNewAgain, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPfPassOld))
                .addContainerGap(54, Short.MAX_VALUE))
        );
        jXPanel3Layout.setVerticalGroup(
            jXPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel3Layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addGroup(jXPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPfPassOld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jXPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jXLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPfPassNew, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jXPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPfPassNewAgain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jXPanel1.add(jXPanel3, java.awt.BorderLayout.CENTER);

        jxBtnCancel.setText("Hủy bỏ");
        jxBtnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jxBtnCancelActionPerformed(evt);
            }
        });

        jxBtnSave.setText("Lưu mật khẩu");
        jxBtnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jxBtnSaveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jXPanel4Layout = new javax.swing.GroupLayout(jXPanel4);
        jXPanel4.setLayout(jXPanel4Layout);
        jXPanel4Layout.setHorizontalGroup(
            jXPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jXPanel4Layout.createSequentialGroup()
                .addGap(153, 153, 153)
                .addComponent(jxBtnSave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(46, 46, 46)
                .addComponent(jxBtnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(70, Short.MAX_VALUE))
        );
        jXPanel4Layout.setVerticalGroup(
            jXPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel4Layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addGroup(jXPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jxBtnSave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jxBtnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(17, Short.MAX_VALUE))
        );

        jXPanel1.add(jXPanel4, java.awt.BorderLayout.SOUTH);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jXPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jXPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jxBtnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jxBtnSaveActionPerformed
        checkChangePassword();
    }//GEN-LAST:event_jxBtnSaveActionPerformed

    private void jxBtnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jxBtnCancelActionPerformed
       this.dispose();
    }//GEN-LAST:event_jxBtnCancelActionPerformed

    private void jPfPassNewAgainKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jPfPassNewAgainKeyPressed
        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
        {
            checkChangePassword();
        }
    }//GEN-LAST:event_jPfPassNewAgainKeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPasswordField jPfPassNew;
    private javax.swing.JPasswordField jPfPassNewAgain;
    private javax.swing.JPasswordField jPfPassOld;
    private org.jdesktop.swingx.JXLabel jXLabel1;
    private org.jdesktop.swingx.JXLabel jXLabel2;
    private org.jdesktop.swingx.JXLabel jXLabel3;
    private org.jdesktop.swingx.JXLabel jXLabel4;
    private org.jdesktop.swingx.JXPanel jXPanel1;
    private org.jdesktop.swingx.JXPanel jXPanel2;
    private org.jdesktop.swingx.JXPanel jXPanel3;
    private org.jdesktop.swingx.JXPanel jXPanel4;
    private org.jdesktop.swingx.JXButton jxBtnCancel;
    private org.jdesktop.swingx.JXButton jxBtnSave;
    // End of variables declaration//GEN-END:variables
}
